<template>
  <div class="main">
    <myHeader/>
    <div class="describe">
      <div class="describe_content">
        <div class="describe_content_title">
          <h1 class="describe_content_title_show" align="center">
            {{ problemId }}.{{ problemName }}
          </h1>
        </div>
        <div class="describe_content_problem_show">
          <div class="describe_content_problem_describe_show">
            <h3>题目描述</h3>
            <div class="describe_content_problem_describe_content">
              <span>{{ problemDescribe }}</span>
            </div>
          </div>
          <div class="describe_content_problem_example">
            <div class="describe_content_problem_example_show">
              <h3>示例</h3>
              <div class="describe_content_problem_example_content">
                <el-card class="box-card" shadow="never">
                  {{ problemExample }}
                </el-card>
              </div>
            </div>
          </div>
        </div>
        <div class="describe_content_problem_limit">
          <div class="describe_content_problem_limit_show">
            <h3>限制要求</h3>
            <div class="describe_content_problem_limit_content">
              <h4>时间限制</h4><br>
              <span>{{timeLimit}}</span>
              <h4>空间限制</h4><br>
              <span>{{memoryLimit}}</span>
            </div>
          </div>

        </div>
      </div>
    </div>
    <div class="editor">
      <div class="editor_head">
        <div class="choose_language">
          <el-select
            class="code-mode-select"
            v-model="mode"
            @change="changeMode"
          >
            <el-option
              v-for="mode in modes"
              :key="mode.value"
              :label="mode.label"
              :value="mode.value"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="editor_content">
        <textarea ref="textarea"></textarea>
      </div>
      <div class="editor_footer">
        <div class="button_group">
          <el-button size="medium" type="primary">保存</el-button>
          <el-button size="medium" type="primary" @click.native="submitCode()"
            >提交</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import myHeader from "@/components/header.vue";
// 引入全局实例
import _CodeMirror from "codemirror";

// 核心样式
import "codemirror/lib/codemirror.css";
// 引入主题后还需要在 options 中指定主题才会生效
import "codemirror/theme/cobalt.css";
import "codemirror/theme/base16-light.css";

// 需要引入具体的语法高亮库才会有对应的语法高亮效果
// codemirror 官方其实支持通过 /addon/mode/loadmode.js 和 /mode/meta.js 来实现动态加载对应语法高亮库
// 但 vue 貌似没有无法在实例初始化后再动态加载对应 JS ，所以此处才把对应的 JS 提前引入
import "codemirror/mode/javascript/javascript.js";
import "codemirror/mode/css/css.js";
import "codemirror/mode/xml/xml.js";
import "codemirror/mode/clike/clike.js";
import "codemirror/mode/markdown/markdown.js";
import "codemirror/mode/python/python.js";
import "codemirror/mode/r/r.js";
import "codemirror/mode/shell/shell.js";
import "codemirror/mode/sql/sql.js";
import "codemirror/mode/swift/swift.js";
import "codemirror/mode/vue/vue.js";

// 尝试获取全局实例
const CodeMirror = window.CodeMirror || _CodeMirror;

export default {
  name: "ProblemDetail",
  props: {
    // 外部传入的内容，用于实现双向绑定
    value: String,
    // 外部传入的语法类型
    language: {
      type: String,
      default: null,
    },
  },
  components: {
    myHeader,
  },
  data() {
    return {
      // 内部真实的内容
      code: "test",
      // 默认的语法类型
      mode: "javascript",
      // 编辑器实例
      coder: null,
      // 默认配置
      options: {
        // 缩进格式
        tabSize: 2,
        // 主题，对应主题库 JS 需要提前引入
        theme: "base16-light",
        // 显示行号
        lineNumbers: true,
        line: true,
      },
      // 支持切换的语法高亮类型，对应 JS 已经提前引入
      // 使用的是 MIME-TYPE ，不过作为前缀的 text/ 在后面指定时写死了
      modes: [
        {
          value: "css",
          label: "CSS",
        },
        {
          value: "javascript",
          label: "Javascript",
        },
        {
          value: "html",
          label: "XML/HTML",
        },
        {
          value: "x-java",
          label: "Java",
        },
        {
          value: "x-objectivec",
          label: "Objective-C",
        },
        {
          value: "x-python",
          label: "Python",
        },
        {
          value: "x-rsrc",
          label: "R",
        },
        {
          value: "x-sh",
          label: "Shell",
        },
        {
          value: "x-sql",
          label: "SQL",
        },
        {
          value: "x-swift",
          label: "Swift",
        },
        {
          value: "x-vue",
          label: "Vue",
        },
        {
          value: "markdown",
          label: "Markdown",
        },
      ],
      userCodeTemplate: "",
      problemDescribe: "",
      problemExample: "",
      problemId: "",
      problemName: "",
      tag: "",
      timeLimit: "",
      memoryLimit: "",
      difficulty: "",
    };
  },
  mounted() {
    let that = this;
    // 初始化
    window.fullHeight = document.documentElement.clientHeight;
    window.fullWidth = document.documentElement.clientWidth;
    that.windowHeight = window.fullHeight; // 高
    that.windowWidth = window.fullWidth; // 宽
    const routerParams = this.$route.query.problem_id;
    this.problemId = routerParams;
    this.coder = CodeMirror.fromTextArea(this.$refs.textarea, this.options);
    this.$axios({
      method: "GET",
      url:
        "http://81.70.192.34:8088/problem/detail/problem?problemId=" +
        routerParams,
    })
      .then((res) => {
        //赋值过程
        this.userCodetTemplate = res.data.data.userCodeTemplate;
        this.coder.setValue(
          this.userCodetTemplate != undefined
            ? res.data.data.userCodeTemplate
            : "usercode"
        );
        this.timeLimit = res.data.data.timeLimit;
        this.problemName = res.data.data.problemName;
        this.problemExample = res.data.data.problemExample;
        this.problemDescribe = res.data.data.problemDescribe;
        this.tag = res.data.data.tag;
        this.difficulty = res.data.data.difficulty;
        this.memoryLimit = res.data.data.memoryLimit;
      })
      .catch((error) => {
        console.error(error);
      });
    this.coder.setCursor(2);
    this.coder.setSize(
      (that.windowWidth / 100) * 63,
      (that.windowHeight / 100) * 79
    );
    // 支持双向绑定
    this.coder.on("change", (coder) => {
      this.code = coder.getValue();

      if (this.$emit) {
        this.$emit("input", this.code);
      }
    });
    // 尝试从父容器获取语法类型
    if (this.language) {
      // 获取具体的语法类型对象
      let modeObj = this._getLanguage(this.language);

      // 判断父容器传入的语法是否被支持
      if (modeObj) {
        this.mode = modeObj.label;
      }
    }
  },
  methods: {
    // 初始化
    _initialize(height, width) {
      // 初始化编辑器实例，传入需要被实例化的文本域对象和默认配置
      this.coder = CodeMirror.fromTextArea(this.$refs.textarea, this.options);
      // 编辑器赋值
      this.coder.setValue(
        this.userCodetTemplate != undefined ? this.userCodeTemplate : "usercode"
      );
      this.coder.setCursor(2);
      this.coder.setSize((width / 100) * 63, (height / 100) * 79);

      // 支持双向绑定
      this.coder.on("change", (coder) => {
        this.code = coder.getValue();

        if (this.$emit) {
          this.$emit("input", this.code);
        }
      });

      // 尝试从父容器获取语法类型
      if (this.language) {
        // 获取具体的语法类型对象
        let modeObj = this._getLanguage(this.language);

        // 判断父容器传入的语法是否被支持
        if (modeObj) {
          this.mode = modeObj.label;
        }
      }
    },
    // 获取当前语法类型
    _getLanguage(language) {
      // 在支持的语法类型列表中寻找传入的语法类型
      return this.modes.find((mode) => {
        // 所有的值都忽略大小写，方便比较
        let currentLanguage = language.toLowerCase();
        let currentLabel = mode.label.toLowerCase();
        let currentValue = mode.value.toLowerCase();

        // 由于真实值可能不规范，例如 java 的真实值是 x-java ，所以讲 value 和 label 同时和传入语法进行比较
        return (
          currentLabel === currentLanguage || currentValue === currentLanguage
        );
      });
    },
    // 更改模式
    changeMode(val) {
      // 修改编辑器的语法配置
      this.coder.setOption("mode", `text/${val}`);

      // 获取修改后的语法
      let label = this._getLanguage(val).label.toLowerCase();

      // 允许父容器通过以下函数监听当前的语法值
      this.$emit("language-change", label);
    },

    submitCode() {
      console.log(this.code);
      this.$axios({
        method: "POST",
        url:
          "http://81.70.192.34:8088/oj/submitCode/title/" +
          "test_java" +
          "/userId/" +
          "1803050128" +
          "/language/" +
          "Java",
        data: {
          code: this.code,
        },
        headers: {
          "Content-Type": "application/json",
        },
      })
        .then((res) => {
          console.log(res);

        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script>

<style scoped>
.main {
  display: flex;
  flex-wrap: wrap;
  height: 100vh;
  width: 100%;
}
.describe {
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  margin-left: 3vw;
  height: 90vh;
  width: 28vw;
}
.describe_content {
}
.describe_content_title {
  height: 5vh;
}
.describe_content_title_show {
  line-height: 5vh;
}
.describe_content_title_show {
  border-bottom: 1px solid #e6e6e6;
}
.describe_content_problem_show {
  height: 40vh;
  margin-left: 1vw;
}
.describe_content_problem_describe_show {
  height: 14vh;
}
.describe_content_problem_example {
  height: 20vh;
}
.box-card{
  height: 20vh;
  width: 26vw;
}
.describe_content_problem_limit{
  margin-left: 1vw;
  margin-top: 1vh;
  height: 20vh;
}
.editor {
  margin-left: 3vh;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  display: flex;
  flex-wrap: wrap;
  height: 90vh;
  width: 63vw;
}
.editor_head {
  height: 6vh;
  width: 63vw;
  border-bottom: 1px solid #e6e6e6;
}
.choose_language{
  margin-left: 1vw;
  line-height: 6vh;
}
.editor_content {
  height: 78vh;
  width: 63vw;
  border-bottom: 1px solid #e6e6e6;
}
.editor_footer {
  height: 6vh;
  width: 63vw;
}
.button_group {
  float: right;
  line-height: 6vh;
  margin-right: 2vw;
}
.el-dropdown {
  vertical-align: top;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.CodeMirror-scroll {
  overflow: scroll !important;
  margin-bottom: 0;
  margin-right: 0;
  padding-bottom: 0;
  height: 78vh;
  width: 63vw;
  outline: none;
  position: relative;
  border: 1px solid #dddddd;
}
</style>
